package by.twouexplorer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class TextEditor extends Activity 
{
	
	public static final int IDM_OPEN = 101; 
	public static final int IDM_SAVE = 102; 
	
	
	private String FILENAME;// = getIntent().getExtras().getString(Intent.EXTRA_TEXT); 
	private EditText myEdit;
	private File openedTxtFile;

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_editor);
        myEdit = (EditText)findViewById(R.id.editText); 
        FILENAME = getIntent().getExtras().getString(Intent.EXTRA_TEXT);
        openedTxtFile = new File(FILENAME);
        openFile(openedTxtFile);
    }
    
   
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
        //menu.add(Menu.NONE, IDM_OPEN, Menu.NONE, "Open"); 
            
        menu.add(Menu.NONE, IDM_SAVE, Menu.NONE, "Save")
            .setIcon(android.R.drawable.ic_menu_save);
                    
        return(super.onCreateOptionsMenu(menu)); 
    }
    
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch (item.getItemId())
        {
            //case IDM_OPEN: 
              //  openFile(jopa); 
               // break; 
            case IDM_SAVE: 
                saveFile(openedTxtFile); 
                break; 
            default: 
                return false; 
        }
          return true; 
    }
    
    
    public void openFile(File fileName)
    {
		try
		{
			InputStream inStream = new FileInputStream(fileName);
			
			if(inStream != null)
			{
				InputStreamReader sr = new InputStreamReader(inStream);
				BufferedReader reader = new BufferedReader(sr);
				String str;
				StringBuffer buffer = new StringBuffer();
				
				while((str = reader.readLine()) != null)
				{
					buffer.append(str + "\n");
				}
				
				inStream.close();
				myEdit.setText(buffer.toString());
			}
		}
		catch(Throwable t)
		{
			Toast.makeText(getApplicationContext(), 
					"Exception: " + t.toString(), Toast.LENGTH_LONG).show();
		}
	}
    
   
    
    private void saveFile(File editedFileToSave) 
    {
        try 
        { 
            OutputStream outStream = new FileOutputStream(editedFileToSave);  
            OutputStreamWriter sw = new OutputStreamWriter(outStream); 
            sw.write(myEdit.getText().toString()); 
            sw.close(); 
        }
        catch (Throwable t) 
        {
            Toast.makeText(getApplicationContext(), 
                "Exception: " + t.toString(), Toast.LENGTH_LONG) 
            .show(); 
        }
    }
}